<script type="text/javascript">
	var login_user_id = 0;
	
	var cur_pageNo = 1;
	var cur_pageTotals = 1;
	// Loading flag
	var loading = false;
	
	function delLife(id){
		if (confirm("确定要删除码？")){
			var surl = '/front/teacher/lifepieces/del?id=' + id;
			sendPost(surl, null, function(res){
				alert(res.msg);
				if (res.code == 200){
					$$('#tbl_row_' + id).remove();
				}
			});
		}
		return false;
	}
	function showPhotobw(id){
		var arryimg = trim($$('#tbl_row_'+id).attr('data-imgs'), ';').split(';')
		var myPhotoBrowser = app.photoBrowser.create({
			photos : arryimg,
			theme: 'dark'
		});
		myPhotoBrowser.open();
		return false;
	}
	function bindEvent(){
		$$('a.btn-more,img.col-33-img').off('click', showPhotosPanel);
		$$('a.btn-more,img.col-33-img').on('click', showPhotosPanel);
	}
	function showPhotosPanel(){
		var id = $$(this).attr('data-id');
		var arryimg = trim($$('#tbl_row_'+id).attr('data-imgs'), ';').split(';')
		var myPhotoBrowser = app.photoBrowser.create({
			photos : arryimg,
			theme: 'dark'
		});
		myPhotoBrowser.open();
		return false;
	}
	
	// 监听滚动事件
	$$('.infinite-scroll-content').on('infinite', function () {
	    // Exit, if loading in progress
		if (loading) return;

		if (checkEndPage()){
			return;
		}

		// Set loading flag
		loading = true;
	
	    getPage(cur_pageNo+1);
	});
	
	function checkEndPage(){
		if (cur_pageNo>=cur_pageTotals){
			app.infiniteScroll.destroy('.infinite-scroll-content');
			$$('.infinite-scroll-preloader').html('没有更多数据了！')
											.addClass('loaded')
											.removeClass('preloader')
											.removeClass('infinite-scroll-preloader');
			
			return true;
		}else{
			return false;
		}
	}
	
	function getPage(pageNo){
		var surl = "/front/teacher/lifepieces/getPage?pageNo=" + pageNo + "&pageSize=" + global_pagesize+ "&typeid=2&onlyme="+onlyme;
		sendPost(surl, null, function(res){
			//console.log(res);
			loading = false;
			cur_pageNo = pageNo;
			if (pageNo == 1){
				$$('#ul_list_live').html('');
				login_user_id = res.login_teacher_rd.user_id;
				$$('#top_org_name').text(res.login_teacher_rd.org_name + formatNullValue(res.login_teacher_rd.class_name));
			}
			if (res.pageData.list.length>0){
				cur_pageTotals = res.pageData.totalPage;
				var shtml = '';
				for (var i = 0; i < res.pageData.list.length; i++) {
					var jdata = res.pageData.list[i];
					shtml += getTemplateHtml(jdata);
				}
				$$('#ul_list_live').append(shtml);
			}
			
			// 绑定事件
			bindEvent();
			
			checkEndPage();
		});
	}
	
	function getTemplateHtml(jdata){
		//console.log(jdata);
		var shtml = '';
		var imghtml = "";
		var lablehtml = "";
		// 标签
		if (!isBlank(jdata.picese_tag)){
			lablehtml += '<label class="label-info">'+jdata.picese_tag+'</label>';
		}
		if (!isBlank(jdata.picese_imgs)){
			var arryimg = trim(jdata.picese_imgs, ';').split(';');
			for(var j=0; j<arryimg.length; j++){
				imghtml += '<div class="col-33"><img onclick="showPhotobw('+jdata.picese_id+')" class="col-33-img" src="'+ getZoomFileUrl(arryimg[j])+'" data-id="'+jdata.picese_id+'"></div>';
				if (j>=2){ break; }
			}
			if (arryimg.length > 3){
				if (lablehtml.length > 0){ lablehtml += '<br>'; }
				lablehtml += '<label class="label-info">已上传至班级相册</label>';
				lablehtml += '<a href="javascript:showPhotobw('+jdata.picese_id+')" class="button button-small btn-more" data-id="'+jdata.picese_id+'">详情</a>';
			}
		}
		if (login_user_id == jdata.add_userid){
			lablehtml += '<a href="javascript:delLife('+jdata.picese_id+')" class="button button-small a-btn-del" data-id="'+jdata.picese_id+'">删除</a>';
		}
		
		shtml += '<li class="block active-block">';
		shtml += `
				<div id="tbl_row_${jdata.picese_id}" class="row" data-imgs="${jdata.picese_imgs}">
					<div class="col-15">
						<span class="head-portrait"><img src="${formatUserHeadImage(jdata.user_img)}"></span>
					</div>
					<div class="col-85">
						<div class="block-header"><b>${jdata.user_name}</b></div>
						<div class="block-content">
							<p>${formatNullValue(jdata.picese_info)}</p>
							<div class="row section-right-row">
								${imghtml}
							</div>
						</div>
						<div class="block-footer">
							<label class="label-time">${formatDate(jdata.add_time, 'MM-dd hh:mm')}</label>
							${lablehtml}
						</div>
					</div>
				</div>
				`;
		shtml += '</li>';
		return shtml;
	}
	
	getPage(1);
</script>